local Lua__ronghe = fk.CreateSkill {

  name = "Lua__ronghe",

}

local U = require "packages.utility.utility"

Fk:loadTranslationTable{
    ["Lua__ronghe"] = "融合",
    ["$Lua__ronghe1"] = "融合1",
    ["$Lua__ronghe2"] = "融合2",
    [":Lua__ronghe"] = "每回合每种牌名限一次，你可以将两张手牌当做任意一张基本牌或锦囊牌使用或打出，然后回复一点体力并对所有角色造成一点伤害。",
    ["#Lua__ronghe"] = "融合：你可以将两张手牌当做任意一张基本牌或锦囊牌使用或打出",
    ["#ronghe-choice"] = "融合：请选择要转化成的牌",
}


Lua__ronghe:addEffect("viewas", {
    pattern = ".",
    interaction = function(self, player)
        local mark = player:getTableMark("ronghe-turn")
        local all_names = Fk:getAllCardNames("btd")
        local names = table.filter(player:getViewAsCardNames(Lua__ronghe.name, all_names), function (name)
            local card = Fk:cloneCard(name)
            return not table.contains(mark, card.trueName)
          end)
          if #names > 0 then
            return UI.CardNameBox { choices = names, all_choices = all_names }
          end
    end,
    handly_pile = true,
    card_filter = function(self, player, to_select, selected)
        return #selected < 2
    end,
    view_as = function(self, player,cards)
        if #cards ~= 2 or not self.interaction.data then return end
        local card = Fk:cloneCard(self.interaction.data)
        card:addSubcards(cards)
        card.skillName = Lua__ronghe.name
        return card
    end,
    enabled_at_play = function(self, player)
        return true
    end,
    enabled_at_response = function(self, player, response)
        if response or #player:getHandlyIds() < 2 then return false end
        local mark = player:getTableMark("ronghe-turn")
        return #table.filter(player:getViewAsCardNames(Lua__ronghe.name,Fk:getAllCardNames("btd")), function (name)
          return not table.contains(mark, Fk:cloneCard(name).trueName)
        end) > 0
    end,
    on_acquire = function (self, player, is_start)
        local mark = {}
        local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn)
        if turn_event == nil then return end
        local use
        player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
          use = e.data
          if use.from == player then
            table.insertIfNeed(mark, use.card.trueName)
          end
          return false
        end, turn_event)
        player.room:setPlayerMark(player, "ronghe-turn", mark)
      end,
})

Lua__ronghe:addEffect(fk.CardUsing, {
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return not player.dead and player == target and table.contains(data.card.skillNames, Lua__ronghe.name)
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        player.room:addTableMarkIfNeed(player, "ronghe-turn", data.card.trueName)
    end,
})
Lua__ronghe:addEffect(fk.CardUseFinished, {
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return not player.dead and player == target and table.contains(data.card.skillNames, Lua__ronghe.name)
      end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:recover({
            who = player,
            num = 1,
            recoverBy = player,
            skillName = "Lua__ronghe"
        })
        -- 对所有角色造成伤害
        for _, p in ipairs(room:getAlivePlayers()) do
            room:damage({
                from = player,
                to = p,
                damage = 1,
                skillName = "Lua__ronghe"
            })
        end
    end,
})
Lua__ronghe:addEffect(fk.CardRespondFinished, {
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return not player.dead and player == target and table.contains(data.card.skillNames, Lua__ronghe.name)
      end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:recover({
            who = player,
            num = 1,
            recoverBy = player,
            skillName = "Lua__ronghe"
        })
        for _, p in ipairs(room:getAlivePlayers()) do
            room:damage({
                from = player,
                to = p,
                damage = 1,
                skillName = "Lua__ronghe"
            })
        end
    end,
})

return Lua__ronghe